package com.charles.seckillcommon.core.domain;

import java.util.HashMap;
import java.util.Objects;

/**
 * @author Charles-H
 * 
 * 客户端响应码
 */
public class AjaxResult extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    public static final String CODE_TAG = "code";

    public static final String MSG_TAG = "msg";

    public static final String DATA_TAG = "data";

    public AjaxResult() {
    }

    public AjaxResult(Integer code, String msg) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
    }

    public AjaxResult(Integer code, String msg, Object data) {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }

    public static AjaxResult success() {
        return AjaxResult.success("成功");
    }

    public static AjaxResult success(Object data) {
        return AjaxResult.success("成功", data);
    }

    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }

    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(200, msg, data);
    }

    public static AjaxResult error() {
        return AjaxResult.error("失败");
    }

    public static AjaxResult error(Object data) {
        return AjaxResult.error("失败", data);
    }

    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg, null);
    }

    public static AjaxResult error(Integer code, String msg) {
        return new AjaxResult(code, msg, null);
    }

    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(500, msg, data);
    }

    public boolean isSuccess() {
        return !isError();
    }

    public boolean isError() {
        return Objects.equals(200, this.get(CODE_TAG));
    }

    @Override
    public Object put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
